/* 
 * map-jquery.js
 */

$(function(){
    // Initialisation de la carte
    mapInitialise();
    // Ajouter un itinéraire
    $('#itineraire').click(function(e){
        e.preventDefault();
        addItinerary();
    });
    // Rechercher
    $('#search').keyup(function(){
        console.log("Recherche de l'emplacement : '" + $(this).val() + "'");
        geocoder.geocode({address : $(this).val()}, function(results, status){
            if(status == google.maps.GeocoderStatus.OK){
//                console.log("Nouveau Centre");
//                console.log(results[0].geometry.location);
//                map.setCenter(results[0].geometry.location);
                searchResult = results;
                $('#search').popover('show');
            }else{
                $('#search').popover('hide');
            }
        });
    });
    // Afficher / Cacher
    $('#search').click(function(){
        $('#search').popover('toggle');
    });
    
    // Popover
    $('#search').popover({
        placement: 'bottom',
        trigger: 'manual',
        title: 'Résultats de recherche',
        html: true,
        content: function(){
            var count = searchResult.length;
            var $content = $('<div>');
            $content.html('');
            for(var i = 0; i < count; i++){
                var result = searchResult[i];
                console.log(result);
                var $a = $('<a>', {
                    href: '#',
                    text: result.formatted_address,
                    'class': 'research-result',
                    'data-lat': result.geometry.location.lat(),
                    'data-lng': result.geometry.location.lng()
                });
                $content.append($a);
                $content.append('<br>');
            }
            return $content.html();
        }
    });
    // Déplacer la carte
    $(document).on('click', '.research-result', function(e){
        e.preventDefault();
        var lat = $(this).attr('data-lat');
        var lng = $(this).attr('data-lng');
        var latLng = new google.maps.LatLng(lat, lng);
        map.setCenter(latLng);
        $('#search').popover('hide');
    });
    // Itinéraire par défaut
    $('#itineraire-defaut').click(function(e){
        e.preventDefault();
        var type = parseInt($('#defaut-type').val());
        drawDefaultItinerary(type);
    });
    // Itinéraire par nom
    $('#itineraire-nom').click(function(e){
        e.preventDefault();
        var type = parseInt($('#name-type').val());
        var names = getNames();
        console.log(names);
        var count = names.length;
//        var marqueurs = new Array();
        var $this = $(this);
        var oldText = $this.text();
        //$this.text("Recherche des différents points ...");
        emptyTheNames();
        for(var i = 0; i < count; i++){
            console.log("Recherche de " + names[i]);
            geocoder.geocode({address : names[i]}, function(results, status){
                if(status == google.maps.GeocoderStatus.OK){
                    console.log(results[0].formatted_address + " trouvé");
                    console.log(results);
                    var result = results[0];
                    addMarker(result.geometry.location, default_image, results[0].formatted_address);
                    markersForName.push(markers[markerIndex - 1]);
                    console.log("Dessin des polylines");
                    drawPolyLinesForName(type);
                }else{
                    console.log(name + " introuvable");
                    var $msg = $('<div>');
                    $msg.html("Le lieux <b>'" + name + "'</b> est introuvable");
                }

            });
        }
    });
});
